const app = getApp();
// const controllers = app.controllers;
const controller=require("../../../controllers/ControllerVip").controller;
const controllers=require("../../../controllers/Controller").controller

Page({
  data: {
    id:"",
    isShare:0,
    dataInfo:{},
    show:false,
    giveNum:"",
    showBox:false,
    giveId:"", 
    receiveId:"",
    isShopCircleActive:0
  },

  //返回首页
  backindex(e){
    let page=getCurrentPages()
    let shopId=this.data.shopId
    console.log(page.length)
    if(page.length<=1){
      wx.reLaunch({
        url: '/pages/TradingArea/index',
      })
    }else{
      wx.navigateBack()
    }
  },

  // 返回店铺首页
  toShopIndex(e){
    let shopId=this.data.shopId
    wx.reLaunch({
      url: '/pages/Index/Index?shopId='+shopId,
    })
  },

  //取消弹框
  cancleBox(){
    this.setData({
      showBox:false,
      show:false
    })
  },

  //跳转优惠券详情
  toDetail(e){
    let id=""
    if(this.data.receiveId){
      id=e.currentTarget.dataset.goodsid
    }else{
      id=e.currentTarget.dataset.id
    }
    let couponAccountId=e.currentTarget.dataset.haveid||''
    let couponType=e.currentTarget.dataset.paytype
    let couponBagId=e.currentTarget.dataset.couponbagid
    let shopId=e.currentTarget.dataset.shopid
    if(this.data.dataInfo.vip.type==4){
      wx.navigateTo({
        url: '/pages/Mall/couponDetailBuy?id='+id+"&couponAccountId="+couponAccountId+'&couponBagId='+couponBagId+"&out=1"+"&shopId="+shopId,
      })
    }else{
      wx.navigateTo({
        url: '/pages/Mall/Coupondetail?id='+id+"&couponAccountId="+couponAccountId+"&shopId="+shopId,
      })
    }
  },

  //获取赠送数量
  sendNum(e){
    let num=e.detail.value
    num=num.replace(/[^0-9]/g, '')
    this.setData({
      giveNum:num
    })
  },

  //领取亲友卡
  receiveGiveVip(){
    let value={}
    if(this.data.isShare){//分享
      value.vipGiveRecordId=this.data.receiveId
      controllers.receiveGiveVip(value).then(res=>{
        this.toMyVipCardPage()
      })
    }else{//二维码
      value.code=this.data.receiveId
      controllers.receiveGiveVipByCode(value).then(res=>{
        this.toMyVipCardPage()
      })
    }
  },
  toMyVipCardPage(){
    wx.showToast({
      title: '领取成功',
    })
    setTimeout(function(){
      wx.reLaunch({
        url: '/pages/my/vip/vip_myCard/index',
      })
    },1500)
  },
  // 生成赠送二维码
  sendQR(){
    const value = {
      accountVipId:this.data.id
    }
    controllers.shareAccountVip(value).then(res=>{
      wx.previewImage({
        urls: [res.data],
      })
    })
  },

  //打开弹框
  openDialog(){
    if(!this.data.giveNum){
      wx.showToast({
        icon:"none",
        title: '请输入赠送数量',
      })
      return
    }
    if(this.data.giveNum>this.data.dataInfo.giveNum){
      wx.showToast({
        icon:"none",
        title: '赠送数量不能大于持有数',
      })
      return
    }
    this.setData({
      showBox:true
    })
    this.createGiveId()
  },

  //生成赠送码
  createGiveId(){
    let value={}
    value.shopId=this.data.shopId
    value.accountVipId=this.data.id
    value.giveNum=this.data.giveNum
    controllers.createGiveId(value).then(res=>{
      this.setData({
        giveId:res.data.id
      })
    })
  },

  onLoad: function (options) {
    this.setData({
      isShopCircleActive:options.isShopCircleActive
    })
    if(options.receiveId){
      this.setData({
        receiveId:options.receiveId
      })
      console.log("receiveId:"+this.data.receiveId)
    }
    if(options.isShare){
      this.setData({
        isShare:options.isShare
      })
    }

    let id = options.id;
    let shopId = app.globalData.shopId;
    if(options.shopId){
      shopId = options.shopId;
    }
     if(options.scene){
        let scene=decodeURIComponent(options.scene);
        console.log(scene);
        let vipId=scene.split(",")[0].split(":")[1];
        let sId=scene.split(",")[1].split(":")[1];
        if(vipId){ 
          id = vipId;
        }
        if(sId){
          shopId = sId; 
        }
     }
    if(shopId){
      wx.setStorageSync('shopId', shopId);
      app.globalData.shopId=shopId;
    }
    this.setData({
      'id':id,
      shopId:shopId
    });
  },

  onShow(){
    if(wx.getStorageSync('usertoken')){
      this.getInfo();
    }else{
      app.usertokenReadyCallback = res => {
        this.getInfo(); 
      }
    }
  },

  getInfo: function () {
  
    let that = this; 

    if(this.data.receiveId){
      let value={}
      value.vipGiveRecordId=this.data.receiveId
      controller.getGiveVipDetail(value).then(res=>{
        that.setData({
          dataInfo:res.data
        })
      })
    }else{
      let d = {
        accountVipId:this.data.id,
        shopId:this.data.shopId
      };
      controller.getVipDetail(d).then(res=>{
        that.setData({
          dataInfo:res.data
        })
      });
    }
  },
  toUrl(e) {
    var d = e.currentTarget.dataset;
    let url = d.url;
    console.info(url);
    wx.navigateTo({
      url: url
    });
  },
  onBack(){
    wx.navigateBack();
  },
  cancle(){
    this.setData({
      show:false
    })
  },
  createCode(e){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.vipGoodsId=e.currentTarget.dataset.id
    controllers.createVipGoodsWriteOffCode(value).then(res=>{
      this.setData({
        code: res.data.code,
        codeUrl:res.data.qrCode,
        show:true
      })
    })
  },
  writeOff(e) {
    let value = {
      couponAccountId:e.currentTarget.dataset.couponaccountid,
      couponBagId:this.data.dataInfo.couponBagId
    }
    controllers.createWriteOffCouponQrCode(value).then(res => {
      this.setData({
        code: res.data.code,
        codeUrl: res.data.qrCode,
        show: true
      })
     
    })
  },

  saveImg: function () {
    let that=this
    wx.getSetting({
      success(res) {
        //未授权 先授权 然后保存
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res) {
              that.saveToBlum();
            }
          })
        }else{
         //已授 直接调用保存到相册方法
          that.saveToBlum();
        }
      }
    })  
  },

  saveToBlum:function(){
    let that=this
    wx.downloadFile({
      url: that.data.codeUrl,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(result) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    console.log("/pages/my/vip/my/VIPCardInfo?id="+this.data.id+"&receiveId="+this.data.giveId+"&shopId="+this.data.shopId)

    return {
      title:"亲友卡",
      path:"/pages/my/vip/my/VIPCardInfo?isShare=1&id="+this.data.id+"&receiveId="+this.data.giveId+"&shopId="+this.data.shopId
    }
  } 
})